package com.lijiaxing.service.impl;

import com.lijiaxing.mapper.codeMapper;
import com.lijiaxing.pojo.Result;
import com.lijiaxing.service.codeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Random;

@Service
public class codeServiceImpl implements codeService {
    @Autowired
    private codeMapper cm;

    @Override
    public Result sendCode(String tel) {
        /**
         * @description 随机生成一个 6位验证码
         * */
        StringBuilder code = new StringBuilder();
        Random random = new Random();
        for (int i = 1; i <= 6; i++) {
            code.append(random.nextInt(10));
        }
        //验证码发送成功
        try {
            int i = cm.addCode(tel, Integer.parseInt(code.toString()));
            if (i > 0) {
                return Result.success(code, "验证码已发送" + code);
            }
            return Result.error("", "发送失败");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("", "验证码已发送请稍后再试");
        }


    }
}
